{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "Initializing_a_tensor.ipynb",
      "provenance": [],
      "include_colab_link": true
    },
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.6"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/PacktPublishing/Modern-Computer-Vision-with-PyTorch/blob/master/Chapter02/Initializing_a_tensor.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "ExecuteTime": {
          "end_time": "2020-09-25T19:01:20.140985Z",
          "start_time": "2020-09-25T19:01:19.854244Z"
        },
        "id": "ihh6_GZ3ZIY-"
      },
      "source": [
        "import torch\n",
        "x = torch.tensor([[1,2]])\n",
        "y = torch.tensor([[1],[2]])"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "ExecuteTime": {
          "end_time": "2020-09-25T19:01:20.144749Z",
          "start_time": "2020-09-25T19:01:20.142079Z"
        },
        "id": "Q3M5XCH9ZJ2c",
        "outputId": "6a17c9f7-69b5-40c7-8a35-b3daf6dbe60e",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 68
        }
      },
      "source": [
        "print(x.shape)\n",
        "# torch.Size([1,2]) # one entity of two items\n",
        "print(y.shape)\n",
        "# torch.Size([2,1]) # two entities of one item each\n",
        "print(x.dtype)\n",
        "# torch.int64"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "torch.Size([1, 2])\n",
            "torch.Size([2, 1])\n",
            "torch.int64\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "ExecuteTime": {
          "end_time": "2020-09-25T19:01:20.149178Z",
          "start_time": "2020-09-25T19:01:20.146083Z"
        },
        "id": "BTEN4-yhZL4j",
        "outputId": "a753ca92-6614-4de1-bf7a-cbf331b87fd7",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        }
      },
      "source": [
        "x = torch.tensor([False, 1, 2.0])\n",
        "print(x)\n",
        "# tensor([0., 1., 2.])"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "tensor([0., 1., 2.])\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "ExecuteTime": {
          "end_time": "2020-09-25T19:01:20.156974Z",
          "start_time": "2020-09-25T19:01:20.150527Z"
        },
        "id": "csI_Z5TzZO3B",
        "outputId": "7401ad59-1a21-4d5a-c271-54a3036ebc0e",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 68
        }
      },
      "source": [
        "torch.zeros((3, 4))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[0., 0., 0., 0.],\n",
              "        [0., 0., 0., 0.],\n",
              "        [0., 0., 0., 0.]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 4
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "ExecuteTime": {
          "end_time": "2020-09-25T19:01:20.161041Z",
          "start_time": "2020-09-25T19:01:20.157954Z"
        },
        "id": "nocW50iiZUHD",
        "outputId": "da76102a-8218-4ef5-d61a-e32de822431e",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 68
        }
      },
      "source": [
        "torch.ones((3, 4))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[1., 1., 1., 1.],\n",
              "        [1., 1., 1., 1.],\n",
              "        [1., 1., 1., 1.]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 5
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "ExecuteTime": {
          "end_time": "2020-09-25T19:01:20.165239Z",
          "start_time": "2020-09-25T19:01:20.162169Z"
        },
        "id": "owSLyu6CZVke",
        "outputId": "5e155bca-c8b4-4613-a3c2-f50dd41c9beb",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 68
        }
      },
      "source": [
        "torch.randint(low=0, high=10, size=(3,4))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[8, 2, 5, 9],\n",
              "        [6, 1, 6, 0],\n",
              "        [5, 6, 9, 5]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 6
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "ExecuteTime": {
          "end_time": "2020-09-25T19:01:20.169014Z",
          "start_time": "2020-09-25T19:01:20.166159Z"
        },
        "id": "3wz1iubjZXLn",
        "outputId": "2cba953d-6619-4845-97e9-5ea69c06068e",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 68
        }
      },
      "source": [
        "torch.rand(3, 4)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[0.3196, 0.9387, 0.9268, 0.1246],\n",
              "        [0.6700, 0.7529, 0.8687, 0.3948],\n",
              "        [0.2279, 0.2309, 0.0151, 0.0339]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 7
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "ExecuteTime": {
          "end_time": "2020-09-25T19:01:20.174384Z",
          "start_time": "2020-09-25T19:01:20.170128Z"
        },
        "id": "_-_lniB8ZwuX",
        "outputId": "ac7f1f58-5dd8-423b-ffe2-4d4a050f3a72",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 68
        }
      },
      "source": [
        "torch.randn((3,4))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[ 1.3746, -0.2895, -0.4134, -1.8711],\n",
              "        [-0.3113,  0.8154,  0.5902, -0.8436],\n",
              "        [ 0.8125,  0.3806, -0.5974, -0.6515]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 8
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "ExecuteTime": {
          "end_time": "2020-09-25T19:22:06.835458Z",
          "start_time": "2020-09-25T19:22:06.829580Z"
        },
        "id": "xCwiFIwk2Bu2",
        "outputId": "292de0d2-6604-4d2d-dc1c-b8b0967555fd"
      },
      "source": [
        "import numpy as np\n",
        "x = np.array([[10,20,30],[2,3,4]])\n",
        "y = torch.tensor(x)\n",
        "print(type(x), type(y))"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "application/javascript": [
              "\n",
              "        if (window._pyforest_update_imports_cell) { window._pyforest_update_imports_cell('import numpy as np'); }\n",
              "    "
            ],
            "text/plain": [
              "<IPython.core.display.Javascript object>"
            ]
          },
          "metadata": {
            "tags": []
          }
        },
        {
          "output_type": "stream",
          "text": [
            "<class 'numpy.ndarray'> <class 'torch.Tensor'>\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "h1EJkME52Bu4"
      },
      "source": [
        ""
      ],
      "execution_count": null,
      "outputs": []
    }
  ]
}